package util;
import java.util.ArrayList;
import java.util.List;
import com.paypal.api.payments.Amount;
import com.paypal.api.payments.Payer;
import com.paypal.api.payments.Payment;
import com.paypal.api.payments.PaymentExecution;
import com.paypal.api.payments.RedirectUrls;
import com.paypal.api.payments.Transaction;
import com.paypal.base.rest.APIContext;
import com.paypal.base.rest.PayPalRESTException;

	
public class PaypalPayment {
	public static APIContext apiContext = new APIContext(ConfigUtil.clientID, ConfigUtil.clientSecret, ConfigUtil.mode);
    /**Create Payment Object
     * @param total--金额
     * @throws PayPalRESTException 
     */
	public static Payment createPayment(String total) throws PayPalRESTException {
		Payer payer = new Payer();
		payer.setPaymentMethod(ConfigUtil.method);
		Amount amount = new Amount();
		amount.setTotal(total);
		amount.setCurrency(ConfigUtil.currency);
		
		Transaction transaction = new Transaction();
		transaction.setAmount(amount);
		transaction.setNotifyUrl(ConfigUtil.notify_url);
		transaction.setDescription(ConfigUtil.description);
		List<Transaction> transactions = new ArrayList<Transaction>();
		transactions.add(transaction);
		
		
		RedirectUrls redirectUrls = new RedirectUrls();
		redirectUrls.setCancelUrl(ConfigUtil.cancelUrl);
		redirectUrls.setReturnUrl(ConfigUtil.returnUrl);
		
		
		Payment payment = new Payment();
		payment.setPayer(payer);
		payment.setTransactions(transactions);
		payment.setRedirectUrls(redirectUrls);
		payment.setIntent(ConfigUtil.intent);
		System.out.println(payment.create(apiContext).toString());
		return payment.create(apiContext);
	}

    public static Payment executePayment(String paymentId, String payerId) throws PayPalRESTException{
        Payment payment = new Payment();
        payment.setId(paymentId);
        PaymentExecution paymentExecute = new PaymentExecution();
        paymentExecute.setPayerId(payerId);
        return payment.execute(apiContext, paymentExecute);
    }


}